package com.lagou.controller;

import com.lagou.response.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final String STATUS_CODE = "statusCode";

    private static final String MESSAGE = "message";

    private static final String RESPONSE_CODE = "500";

    /**
     * 捕获所有异常后 自定义响应异常信息
     * @param exception 捕获到的异常
     * @return Map<String, String>
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public ResponseResult<Map<String, String>> dealAllException(Exception exception) {
        Map<String, String> exceptionMap = new HashMap<>();
        exceptionMap.put(STATUS_CODE, RESPONSE_CODE);
        exceptionMap.put(MESSAGE, exception.getMessage());
        return ResponseResult.fail(exceptionMap);
    }
}
